<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
</html>

<script>
// 英文：
  // major - 主修
  // static - 静态的

/* 面向对象 - 构造函数规则
  1. 约定 - 首字母大写，叫 构造函数/class
  2. 构造函数生成 对象，要用 new 关键字
  3. 构造函数内部 this 关键字， this 代指  new 时 生成的对象
  4. 构造函数体内不能有 return 关键字
*/
function Teacher(major, name, height, weight) {
  this.major = major;
  this.name = name;
  this.height = height;
  this.weight = weight;
}


// 往构造函数中，增加 实例方法  - 
Teacher.prototype.sayHi = function() {
  console.log(`你好呀 同学, 我叫 ${this.name}`);
}
const laozhang = new Teacher('数学', '老张', '180cm', '70kg');
// laozhang.sayHi();


// 往构造函数中，增加 静态方法
Teacher.isTeacher = function(obj) {
  // obj.__proto__  指向原型对象；等同于 Teacher.prototype
  return obj.__proto__.constructor === Teacher;
}

// 往构造函数中，增加 静态属性
Teacher.nation = 'around the world';


function Student(level, name, height, weight) {
  this.level = level;
  this.name = name;
  this.height = height;
  this.weight = weight;
}

Student.prototype.sayHi = function() {
  console.log(`你好呀，我叫${this.name}`);
}

// const xiaoming  = new Student('3年级', '小米', '150cm', '50kg');


// console.log(Teacher.isTeacher(xiaoming));
// console.log(Teacher.isTeacher(laozhang));



// 案例：给 Number 增加 Number.isNumber 的静态方法
Number.isNumber = function(x) {
  return typeof x === 'number';
}


// ----------------------------  构造函数 换成 class ->  即为 构造函数的语法糖
// 案例：用 class 重写 Student 的 构造函数

// function Student(level, name, height, weight) {
//   this.level = level;
//   this.name = name;
//   this.height = height;
//   this.weight = weight;
// }

// Student.prototype.sayHi = function() {
//   console.log(`你好呀，我叫${this.name}`);
// }


class Student {
  constructor(level, name, height, weight) {
    // 实例属性
    this.level = level;
    this.name = name;
    this.height = height;
    this.weight = weight;
  }

  // 实例方法 ->  绑定到 原型对象上
  sayHi() {
    console.log(`你好呀，我叫${this.name}`);
  }

  // 构造函数的 静态方法
  static isStudent(x) {
    return x.__proto__.constructor === Student;
  }

  // 构造函数的 静态数据
  static nation = '中国🇨🇳';
}

const xiaoming = new Student('9年级', '小明', '172cm', '70kg');
console.log(xiaoming);


</script>